package com.bzy.blade_auth.service.impl;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.bzy.blade_auth.config.util.ParmUtil;
import com.bzy.blade_auth.config.util.UserUtil;
import com.bzy.blade_auth.mapper.UserMapper;
import com.bzy.blade_auth.service.LoginService;
import com.bzy.bladeauthapi.entry.User;
import com.bzy.bladeauthapi.vo.QueryWrapperVO;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {


    @Autowired
    private UserMapper userMapper;


    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    @Override
    public R doLogin(User user) {
        //参数校验;
        String parmResult = ParmUtil.parameterIntended(user, "userName","pwd");
        if(parmResult!=null){
            return R.failed(parmResult);
        }
        User selLoginUser = userMapper.selectOne(QueryWrapperVO.getQueryWrapper(JSON.toJSONString(new QueryWrapperVO().eq("user_name", user.getUserName()))));
        if(ParmUtil.isNull(selLoginUser)){
            return R.failed("不存在当前的用户的数据信息;");
        }
        if(!selLoginUser.getPwd().equals(user.getPwd())){
            return R.failed("密码不正确");
        }
        // 会话登录：参数填写要登录的账号id，建议的数据类型：long | int | String， 不可以传入复杂类型，如：User、Admin 等等
        //填写要登录的账号id
        StpUtil.login(selLoginUser.getId());
        //往当前的会话当中设置用户的数据的信息;
        UserUtil.setUserInfoOfSession(selLoginUser);
        return R.ok(selLoginUser);
    }
}






































